import * as request from '../constant/fetch-request'
import HTTP_CODE from '../constant/http-code'

export const refreshTask = data => ({
  type: 'REFRESH_TASK',
  data
})

export const refreshRunningLogs = runningLogs => ({
  type: 'REFRESH_RUNNING_LOGS',
  runningLogs
})

export const refreshOnlineLanguageRenderDate = data => ({
  type: 'REFRESH_HOMEWORK_QUIZZES',
  data
})

export const refreshOnlineLanguageQuizzes = data => ({
  type: 'REFRESH_HOMEWORK_QUIZZES',
  data
})
export const tutorFollowStudent = myStudent => ({
  type: 'FOLLOW_STUDENT_PROGRAM',
  myStudent
})
export const getTask = (taskId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/v2/tasks/${taskId}`)

      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshTask(res.body))
      }
    })()
  }
}

export const submitAnswer = (data, loadLogs) => {
  return dispatch => {
    (async () => {
      const res = await request.post(`../api/v2/onlineLanguageQuizzes/submission`, data)
      if (res.status === HTTP_CODE.CREATED) {
        loadLogs(res.body.id)
        dispatch(getOnlineLanguageQuizzes(data.assignmentId))
      }
    })()
  }
}

export const getOnlineLanguageQuizzes = (assignmentId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/v2/assignments/${assignmentId}/quizzes`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshOnlineLanguageQuizzes(res.body))
      }
    })()
  }
}

export const getRunningLogs = (taskId, assignmentId, quizId, submissionId, getResult) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/v2/onlineLanguageQuizzes/submissions/${submissionId}/students/tasks/${taskId}/assignments/${assignmentId}/quizzes/${quizId}/runningLog`)
      if (res.status === HTTP_CODE.OK) {
        if (getResult) {
          getResult(res.body)
        }
        dispatch(refreshRunningLogs(res.body))
      }
    })()
  }
}

export const getStudentOnlineLanguageQuizzes = (assignmentId, studentId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/v2/assignments/${assignmentId}/students/${studentId}/quizzes`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshOnlineLanguageRenderDate(res.body))
      }
    })()
  }
}
export const getTutorFollowStudent = (programId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/myStudents/programs/${programId}`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(tutorFollowStudent(res.body))
      }
    })()
  }
}
export const submitQuizSuggestion = (suggestion, callback) => {
  return () => {
    (async () => {
      const res = await request.post(`../api/v2/quizzes/suggestions`, suggestion)
      if (res.status === HTTP_CODE.CREATED) {
        callback()
      }
    })()
  }
}
export const getAssignmentDuration = (taskId, assignmentId, callback) => {
  return () => {
    (async () => {
      const res = await request.get(`../api/v2/tasks/${taskId}/assignments/${assignmentId}`)
      if (res.status === HTTP_CODE.OK) {
        callback(res.body)
      }
    })()
  }
}
export const startAssignment = (userId, taskId, assignmentId) => {
  return () => {
    (async () => {
      await request.post(`../api/v2/tasks/assignments/users`, {userId, taskId, assignmentId})
    })()
  }
}
